<script setup>
import {useForm} from "@inertiajs/vue3";
import useDialog from '@/composables/useDialog'
import useLoading from '@/composables/useLoading'
import useRequest from '@/composables/useRequest'
import {Message} from "@arco-design/web-vue";

const {FormRef, visible, mode, pk, actionName, width} = useDialog()
const {loading, setLoading} = useLoading()

const {createApi, updateApi, showApi} = useRequest('admin.content.article.categories')

const emit = defineEmits(["success", "close"]);

const form = useForm({
    parent_id: null,
    name: '',
    slug: '',
    description: '',
    sort: 50,
    status: 1,
})

const rules = {
    name: [{required: true, message: '请输入分类名称'}],
    slug: [{required: true, message: '请输入分类标识'}],
    status: [{required: true, message: '请选择分类状态'}],
    sort: [{required: true, message: '请输入分类排序'},],
}

const handleSubmit = async () => {
    try {
        setLoading(true)
        const res = await FormRef.value?.validate()
        if (res) return false
        const apiMethod = mode.value === 'edit'
            ? () => updateApi(pk.value, form)
            : () => createApi(form)
        await apiMethod()
        Message.success(`${actionName.value}成功`)
        emit('success')
    } catch (error) {
        return false
    } finally {
        setLoading(false)
    }
}

const handleClose = () => {
    FormRef.value?.resetFields()
    form.reset()
    emit('close')
}

// 打开弹窗
const add = async () => {
    mode.value = 'add'
    visible.value = true
}

const edit = async (id) => {
    try {
        setLoading(true)
        pk.value = id
        const {data} = await showApi(id)
        Object.assign(form, data)
        mode.value = 'edit'
        visible.value = true
    } finally {
        setLoading(false)
    }
}

defineExpose({add, edit})
</script>
<template>
    <a-drawer
        v-model:visible="visible"
        :loading="loading"
        :title="`${actionName}`"
        :width
        @close="handleClose"
        @before-ok="handleSubmit"
    >
        <a-spin :loading="loading" class="w-full">
            <a-form ref="FormRef" :model="form" :rules auto-label-width>
                <a-form-item field="parent_id" label="所属分类">
                    <a-tree-select
                        v-model="form.parent_id"
                        :data="$page.props.options"
                        :fieldNames="{
                          key: 'id',
                          title: 'name',
                          children: 'children'
                        }"
                        allow-clear
                        placeholder="请选择所属分类"
                    />
                </a-form-item>

                <a-form-item field="name" label="名称">
                    <a-input
                        v-model.trim="form.name"
                        :max-length="8"
                        allow-clear
                        placeholder="请输入名称"
                        show-word-limit
                    />
                </a-form-item>

                <a-form-item field="slug" label="标识">
                    <a-input
                        v-model.trim="form.slug"
                        :max-length="8"
                        allow-clear
                        placeholder="请输入标识"
                        show-word-limit
                    />
                </a-form-item>

                <a-form-item field="description" label="描述">
                    <a-input
                        v-model.trim="form.description"
                        :max-length="30"
                        allow-clear
                        placeholder="请输入描述"
                        show-word-limit
                    />
                </a-form-item>

                <a-form-item field="sort" label="排序">
                    <a-input-number v-model="form.sort" :min="1" mode="button" placeholder="请输入排序"/>
                </a-form-item>

                <a-form-item field="status" label="状态">
                    <a-radio-group v-model="form.status">
                        <a-radio :value="1">显示</a-radio>
                        <a-radio :value="0">隐藏</a-radio>
                    </a-radio-group>
                </a-form-item>
            </a-form>
        </a-spin>
    </a-drawer>
</template>

<style scoped>

</style>
